/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.dac.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.repository.dac.DataTypeDacGenContext;
import com.inspur.edp.cef.repository.adaptoritem.AdaptorItem;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public abstract class GetAdaptorGenerator extends ClassMethodGenerator
{

	protected String AdaptorFieldName;
	private String nodeCode;
	protected DataTypeDacGenContext dataTypeDacGenContext;

	public GetAdaptorGenerator(String fieldName, DataTypeDacGenContext parentContext)
	{
		AdaptorFieldName = fieldName;
		dataTypeDacGenContext = parentContext;
	}

	public GetAdaptorGenerator(String fieldName, DataTypeDacGenContext parentContext, String nodeCode)
	{
		AdaptorFieldName = fieldName;
		dataTypeDacGenContext = parentContext;
		this.nodeCode  = nodeCode;
	}

	@Override
	protected String getMethodName()
	{
		return "innerGetAdaptor";
	}

	//protected override Type getReturnType()
	//{
	//    return typeof(IRepositoryAdaptor);
	//}


	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list=new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		return null;
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
//		buildAdaptorStatement(block);
		block.statements().add(buildInitStatement());
		block.statements().add(buildReturnStatement());
		return block;
	}

	private void buildAdaptorStatement(Block block){
//		List<AdaptorItem> adaptorItemList = new ArrayList<>();
		ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
		ParameterizedType parameterizedType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("ArrayList")));
		classInstanceCreation.setType(parameterizedType);
		VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
		variableDeclarationFragment.setName(ast.newSimpleName("adaptorItemList"));
		variableDeclarationFragment.setInitializer(classInstanceCreation);

		this.addImport(AdaptorItem.class.getTypeName());
		ParameterizedType parameterizedType1 = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("List")));
		parameterizedType1.typeArguments().add(ast.newSimpleType(ast.newSimpleName("AdaptorItem")));
		VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
		variableDeclarationStatement.setType(parameterizedType1);
		block.statements().add(variableDeclarationStatement);

//		RootNodeEntityAdaptorItem entityAdaptorItem = new RootNodeEntityAdaptorItem();
		MethodInvocation getDbTypeMethodInvocation = ast.newMethodInvocation();
		getDbTypeMethodInvocation.setName(ast.newSimpleName("getDbType"));
		ClassInstanceCreation entityAdaptorItemCreation = ast.newClassInstanceCreation();
		entityAdaptorItemCreation.setType(ast.newSimpleType(ast.newSimpleName( nodeCode + "BaseAdaptorItem")));
		entityAdaptorItemCreation.arguments().add(getDbTypeMethodInvocation);

		VariableDeclarationFragment entityAdaptorItemDeclarationFragment = ast.newVariableDeclarationFragment();
		entityAdaptorItemDeclarationFragment.setName(ast.newSimpleName("baseAdaptorItem"));
		entityAdaptorItemDeclarationFragment.setInitializer(entityAdaptorItemCreation);
		VariableDeclarationStatement entityAdaptorItemDeclarationStatement = ast.newVariableDeclarationStatement(entityAdaptorItemDeclarationFragment);
		entityAdaptorItemDeclarationStatement.setType(ast.newSimpleType(ast.newSimpleName(nodeCode + "BaseAdaptorItem")));
		block.statements().add(entityAdaptorItemDeclarationStatement);

//		adaptorItemList.add(entityAdaptorItem);
		MethodInvocation methodInvocation = ast.newMethodInvocation();
		methodInvocation.setExpression(ast.newSimpleName("adaptorItemList"));
		methodInvocation.setName(ast.newSimpleName("add"));
		methodInvocation.arguments().add(ast.newSimpleName("baseAdaptorItem"));
		ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
		block.statements().add(expressionStatement);

	}

	protected abstract IfStatement buildInitStatement();

	protected ReturnStatement buildReturnStatement()
	{
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(ast.newSimpleName(AdaptorFieldName));
		return returnStatement;
	}
}
